<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */
namespace Home\Controller;
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;

class SmsController extends BaseController
{

    /**发送   短信验证码
     * @param $mobile
     * @param $code
     */
    public function sms_test($mobile,$code)
    {
//        $mobile = I('mobile');

        vendor('dysms.vendor.autoload');
        Config::load();
        $config = tpCache('sms');
        $sms_product = $config['sms_product'];//签名
        $sms_appkey = $config['sms_appkey'];
        $sms_secretKey = $config['sms_secretKey'];
        $sms_templateCode = $config['sms_templateCode'];//模板id
        $product = 'Dysmsapi';
        $domain = 'dysmsapi.aliyuncs.com';
        $region = 'cn-hangzhou';
        $profile = DefaultProfile::getProfile($region, $sms_appkey, $sms_secretKey);
        DefaultProfile::addEndpoint('cn-hangzhou', 'cn-hangzhou', $product, $domain);
        $acsClient = new DefaultAcsClient($profile);
        $request = new SendSmsRequest();

        // 必填，设置短信接收号码
        $request->setPhoneNumbers($mobile);
        // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $request->setSignName($sms_product);
        // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $request->setTemplateCode($sms_templateCode);
        // 可选，设置模板参数, 假如模板中存在变量需要替换则为必填项
        $request->setTemplateParam(json_encode(Array(  // 短信模板中字段的值
            "code" => $code,
            "product" => $product
        )));
        // 可选，设置流水号
        $request->setOutId("yourOutId");
        // 选填，上行短信扩展码（扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段）
        $request->setSmsUpExtendCode("1234567");
        $acsResponse = $acsClient->getAcsResponse($request);
        $return=json_decode(json_encode($acsResponse),true);
        return $this->sendSmsResult($return,$mobile,$code);

    }






    public function sendSmsResult($return,$mobile,$code){

        if($return['Code']=='OK'){
            $data['mobile']=$mobile;
            $data['code']=$code;
            $data['session_id']=session_id();
            $data['add_time']=time();
            $res=M('sms_log')->add($data);
            if($res){
                $arr['msg']='发送成功！';
                $arr['status']=1;
            }else{
                $arr['msg']='发送失败！';
                $arr['status']=-1;
            }
        }else{
            $arr['msg']='发送失败！';
            $arr['status']=-1;
        }
        return $arr;
    }
}